package com.huawei.crm.web.controller;

import com.alibaba.fastjson.JSON;
import com.huawei.crm.query.CustomerReportQueryObject;
import com.huawei.crm.service.ICustomerReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService service;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo) {
        model.addAttribute("list", service.listCustomerReports(qo));
        return "customerReport/list";
    }

    @RequestMapping("/reportByBar")
    public String reportByBar(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo) {
        model.addAttribute("groupTypeName", qo.getGroupTypeName());


        //获取到所有的分组类型的值
        List<Object> groupTypes = new ArrayList<>();
        List<Object> numbers = new ArrayList<>();
        List<Map<String, Object>> maps = service.listCustomerReports(qo);

        for (Map<String, Object> map : maps) {
            groupTypes.add(map.get("groupType"));
            numbers.add(map.get("number"));
        }
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("numbers", JSON.toJSONString(numbers));
        return "customerReport/reportByBar";
    }


    @RequestMapping("/reportByPie")
    public String reportByPie(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo) {
        model.addAttribute("groupTypeName", qo.getGroupTypeName());


        List<Object> groupTypes = new ArrayList<>();
        List<Map<String, Object>> maps = service.listCustomerReports(qo);

        List<Map<String, Object>> datas = new ArrayList<>();

        for (Map<String, Object> map : maps) {
            groupTypes.add(map.get("groupType"));

            Map<String, Object> data = new HashMap<>();
            data.put("value", map.get("number"));
            data.put("name", map.get("groupType"));
            datas.add(data);
        }
        model.addAttribute("groupTypes", JSON.toJSONString(groupTypes));
        model.addAttribute("datas", JSON.toJSONString(datas));
        return "customerReport/reportByPie";
    }


}
